﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "cJSON.h"
#include "tcpclient.h"
#include "commsignalslot.h"

#include "mof_protocol.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //_client = new TcpClient(this);

    QWidget *p = takeCentralWidget();
    if(p) delete p;

    setDockNestingEnabled(true);
    m_docks.append(ui->logDock);
    m_docks.append(ui->netconDock);

    connect(CommSignalSlot::Instance(), &CommSignalSlot::CommSignalLog, this, &MainWindow::wndSlotRecvLog);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_StartConnBtn_clicked()
{
    const char *char_json = "{\"habit\":\"lol\"}";
    //从缓冲区中解析出JSON结构
    cJSON *json = cJSON_Parse(char_json);
    //将传入的JSON结构转化为字符串 并打印
    char *buf = cJSON_Print(json);

    ui->logTextEdit->appendPlainText(buf);

    free(buf);
    cJSON_Delete(json);

    TcpClient::Intance()->tcpSlotStart(DEF_SERVER_IP, 6037);
}

void MainWindow::wndSlotRecvLog(const QString& log)
{
    ui->logTextEdit->appendPlainText(log);

    if(ui->logTextEdit->blockCount() > 8000) {
        ui->logTextEdit->clear();
    }
}

void MainWindow::on_pushButton_2_clicked()
{
    mof_protocol::Instance()->upData();
}
